//调用高德API 地理编码,从name获取经纬度坐标
const https = require('http');
const db = require('./db');

function getCitiesPosition(data) {
  // console.log(data);
  let options = {};
  let i = 0;

  setTimeout(function funcInside() {
    if (i < data.length) {
      options = {
        hostname: 'restapi.amap.com',
        port: 80,
        path: encodeURI(`/v3/geocode/geo?key=2a71c4cc4764ab17ca28f136a0bb5f6b&address=${data[i].name}&output=JSON`),
        method: 'GET'
      };

      const name = data[i].name;

      // console.log(options.path);

      const req = https.request(options, res => {
        // console.log(`状态码: ${res.statusCode}`);
        res.on('data', (d) => {
          const item = JSON.parse(d);
          // console.log(item);
          if (item.geocodes && item.geocodes[0] && item.geocodes[0].formatted_address) {
            console.log(name + '   ' + item.geocodes[0].formatted_address);
          }
          if (data[i] && item && item.geocodes && item.geocodes[0]) {
            db.updateCitiesPos({ name: name, time: data[i].time, pos: ''}, item.geocodes[0].location);
          }
          // console.log(typeof d);
          // console.log(item.geocodes[0].location);
        });
      })

      req.on('error', error => {
        console.error(error)
      });

      req.end(() => {
        i++;
      });

      setTimeout(funcInside, 200)
    } else {
      console.log('城市pos更新完毕');
      return;
    }
  }, 0);
}

function getCityDetailPosition(data) {
  let options = {};
  let i = 0;

  setTimeout(function funcInside() {
    if (i < data.length) {
      options = {
        hostname: 'restapi.amap.com',
        port: 80,
        path: encodeURI(`/v3/geocode/geo?key=2a71c4cc4764ab17ca28f136a0bb5f6b&address=${data[i].station}&city=${data[i].city}&output=JSON`),
        method: 'GET',
      };

      // console.log(options.path);

      const req = https.request(options, res => {
        // console.log(`状态码: ${res.statusCode}`);
        res.on('data', d => {
          const item = JSON.parse(d);
          // console.log(item);
          if (data[i] && item && item.geocodes && item.geocodes[0]) {
            db.updateCityDetail({ city: data[i].city, station: data[i].station, time: data[i].time, pos: '' }, item.geocodes[0].location);
          }
          // console.log(typeof d);
          // console.log(item.geocodes[0].location);
        });
      })

      req.on('error', error => {
        console.error(error);
      });

      req.end(() => {
        i++;
      });

      setTimeout(funcInside, 200);
    } else {
      console.log('监测站pos更新完毕');
      return;
    }
  }, 300);
}

// console.log('');
//预先执行一次
db.getCities({ name: '北京' }, 'time', (data) => {
  db.getCities({ time: data[0].time }, 'name pos time', getCitiesPosition);
  db.getCityDetail({time: data[0].time}, 'city station pos time', getCityDetailPosition);
});

setTimeout(() => {
  db.getCities({ name: '北京' }, 'time', (data) => {
    db.getCities({ time: data[0].time }, 'name pos time', getCitiesPosition);
    db.getCityDetail({time: data[0].time}, 'city station pos time', getCityDetailPosition);
  });
}, 1000 * 60 * 30);